package com.wbcs.system.fileupload;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.wbcs.config.Config;
import com.wbcs.config.component.application.report.ReportBean;
import com.wbcs.config.component.container.page.PageBean;
import com.wbcs.exception.WbcsRuntimeException;
import com.wbcs.system.WbcsResponse;

public class DataImportReportUpload extends AbsFileUpload
{
    public DataImportReportUpload(HttpServletRequest request)
    {
        super(request);
    }

    @Override
	public void showUploadForm(PrintWriter out)
    {
        String pageid=getRequestString("PAGEID","");
        String reportid=getRequestString("REPORTID","");
        PageBean pbean=Config.getInstance().getPageBean(pageid);
        if(pbean==null)
        {
            throw new WbcsRuntimeException("页面ID："+pageid+"不存在");
        }
        ReportBean rbean=pbean.getReportChild(reportid,true);
        if(rbean==null)
        {
            throw new WbcsRuntimeException("ID为"+pageid+"的页面下不存在ID为"+rbean.getId()+"的报表");
        }
        out.print("<input type='hidden' name='PAGEID' value='"+pageid+"'/>");
        out.print("<input type='hidden' name='REPORTID' value='"+reportid+"'/>");
        out.print(showDataImportFileUpload(rbean.getLstDataImportFileNames()));
    }

    @Override
	public String doFileUpload(List lstFieldItems,Map<String,String> mFormFieldValues,PrintWriter out) throws Exception
    {
        String pageid=mFormFieldValues.get("PAGEID");
        String reportid=mFormFieldValues.get("REPORTID");
        pageid=pageid==null?"":pageid.trim();
        reportid=reportid==null?"":reportid.trim();

        PageBean pbean=Config.getInstance().getPageBean(pageid);
        if(pbean==null)
        {
            throw new WbcsRuntimeException("页面ID："+pageid+"不存在");
        }
        ReportBean rbean=pbean.getReportChild(reportid,true);
        if(rbean==null)
        {
            throw new WbcsRuntimeException("ID为"+pageid+"的页面下不存在ID为"+rbean.getId()+"的报表");
        }
        return uploadDataImportFiles(lstFieldItems,rbean.getLstDataImportItems());
    }
}
